package com.macro.mall.demo.controller;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.demo.dto.EsIndexDto;
import com.macro.mall.demo.service.EsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author：尚志豪
 * @Package：com.macro.mall.demo.controller
 * @Project：mall-swarm-B
 * @name：EsController
 * @Date：2024/10/25 20:37
 */
@RestController
@Tag(name ="es索引操作")
@RequestMapping("/es")
public class EsController {
    @Autowired
    private EsService esService;
    /**
     * 创建索引
     */
    @PostMapping("/indexCreate")
    @Operation(summary = "创建索引")
    public CommonResult indexCreate(@RequestBody EsIndexDto esIndexDto){
        esService.indexCreate(esIndexDto);
        return CommonResult.success(null,"索引创建成功");
    }

    /**
     * 检查索引是否存在
     */
    @GetMapping("isExistIndex/{indexName}")
    public CommonResult isExistIndex(@PathVariable String indexName){
        Boolean b = esService.isExistIndex(indexName);
        return CommonResult.success(b);
    }
    /**
     * 删除索引
     */
    @DeleteMapping("/deleteIndex/{indexName}")
    @Operation(summary = "删除索引")
    public CommonResult deleteIndex(@PathVariable @NotEmpty(message = "索引名称不能为空") String indexName){
        esService.deleteIndex(indexName);
        return CommonResult.success(null,"删除成功");
    }
    /**
     * 查询
     */
    @GetMapping("/getIndex/{indexName}")
    @Operation(summary = "查询索引")
    public CommonResult<Map<String,Object>> getIndex(@PathVariable @NotEmpty(message = "索引名称不能为空") String indexName){
        Map<String,Object> index = esService.getIndex(indexName);
        return CommonResult.success(index);
    }
    /**
     * 修改
     */
    @PostMapping("/upIndex")
    @Operation(summary = "修改索引")
    public CommonResult upIndex(@RequestBody EsIndexDto esIndexDto){
        esService.updIndex(esIndexDto);
        return CommonResult.success(null,"索引更新成功");
    }
}
